package com.jianbing.north.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.jianbing.north.domain.NorthQuesStem;
import com.jianbing.north.domain.NorthVideoDTO;
import com.jianbing.north.domain.NorthVideoQuesDTO;
import com.jianbing.north.mapper.NorthAnswerRecordMapper;
import com.jianbing.north.mapper.NorthQuesStemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jianbing.north.mapper.NorthPart1VideoMapper;
import com.jianbing.north.domain.NorthPart1Video;
import com.jianbing.north.service.INorthPart1VideoService;

/**
 * part1视频Service业务层处理
 * 
 * @author zjw
 * @date 2024-06-22
 */
@Service
public class NorthPart1VideoServiceImpl implements INorthPart1VideoService 
{
    @Autowired
    private NorthPart1VideoMapper northPart1VideoMapper;
    @Autowired
    private NorthAnswerRecordMapper northAnswerRecordMapper;
    @Autowired
    private NorthQuesStemMapper northQuesStemMapper;

    /**
     * 查询part1视频
     * 
     * @param id part1视频主键
     * @return part1视频
     */
    @Override
    public NorthPart1Video selectNorthPart1VideoById(Long id)
    {
        return northPart1VideoMapper.selectNorthPart1VideoById(id);
    }

    /**
     * 查询part1视频列表
     * 
     * @param northPart1Video part1视频
     * @return part1视频
     */
    @Override
    public List<NorthPart1Video> selectNorthPart1VideoList(NorthPart1Video northPart1Video)
    {
        return northPart1VideoMapper.selectNorthPart1VideoList(northPart1Video);
    }

    /**
     * 新增part1视频
     * 
     * @param northPart1Video part1视频
     * @return 结果
     */
    @Override
    public int insertNorthPart1Video(NorthPart1Video northPart1Video)
    {
        return northPart1VideoMapper.insertNorthPart1Video(northPart1Video);
    }

    /**
     * 修改part1视频
     * 
     * @param northPart1Video part1视频
     * @return 结果
     */
    @Override
    public int updateNorthPart1Video(NorthPart1Video northPart1Video)
    {
        return northPart1VideoMapper.updateNorthPart1Video(northPart1Video);
    }

    /**
     * 批量删除part1视频
     * 
     * @param ids 需要删除的part1视频主键
     * @return 结果
     */
    @Override
    public int deleteNorthPart1VideoByIds(Long[] ids)
    {
        return northPart1VideoMapper.deleteNorthPart1VideoByIds(ids);
    }

    /**
     * 删除part1视频信息
     * 
     * @param id part1视频主键
     * @return 结果
     */
    @Override
    public int deleteNorthPart1VideoById(Long id)
    {
        return northPart1VideoMapper.deleteNorthPart1VideoById(id);
    }

    @Override
    public NorthVideoDTO selectRandNorthVideoByUserId(Long userId) {
        if(northAnswerRecordMapper.selectTodayCountByUserIdAndPartAndDifficult(userId, 1L,null) >= 3){
            return null;
        }
        NorthVideoDTO res = new NorthVideoDTO();
        NorthPart1Video northPart1Video = northPart1VideoMapper.selectRandNorthVideoByUserId(userId);
        res.setPath(northPart1Video.getPath());
        NorthQuesStem northQuesStem = new NorthQuesStem();
        northQuesStem.setDialogueId(northPart1Video.getDialogueId());
        List<NorthQuesStem> northQuesStems = northQuesStemMapper.selectNorthQuesStemList(northQuesStem);
        List<NorthVideoQuesDTO> northVideoQuesDTOS = new ArrayList<>();
        for(NorthQuesStem quesStem:northQuesStems){
            northVideoQuesDTOS.add(new NorthVideoQuesDTO(quesStem));
        }
        Collections.shuffle(northVideoQuesDTOS);
        res.setQues(northVideoQuesDTOS);
        return res;
    }
}
